package com.atguigu.demo.tree;

import java.awt.*;

public class BinaryTreeDemo1 {
    public static void main(String[] args) {
//先需要创建一颗二叉树
        BinaryTree1 binaryTree = new BinaryTree1();
        //创建需要的结点
        HeroTree root = new HeroTree(1, "宋江");
        HeroTree node2 = new HeroTree(2, "吴用");
        HeroTree node3 = new HeroTree(3, "卢俊义");
        HeroTree node4 = new HeroTree(4, "林冲");
        HeroTree node5 = new HeroTree(5, "关胜");
        HeroTree node6 = new HeroTree(6, "武松1");
        HeroTree node7 = new HeroTree(7, "武松2");
        HeroTree node8 = new HeroTree(8, "武松3");
        HeroTree node9 = new HeroTree(9, "武松3");

        //说明，我们先手动创建该二叉树，后面我们学习递归的方式创建二叉树
        root.setLeft(node2);
        root.setRight(node3);
        node2.setLeft(node7);
        node2.setRight(node7);
        node3.setRight(node4);
        node3.setLeft(node5);
        node4.setLeft(node6);
        node4.setRight(node9);
        node6.setRight(node8);
        node5.setRight(node9);
        binaryTree.setRoot(root);

        //测试
		System.out.println("前序遍历"); // 1,2,3,5,4
		//binaryTree.preOrder();

     /*   //测试
		System.out.println("中序遍历");
		binaryTree.infixOrder(); // 2,1,5,3,4

		System.out.println("后序遍历");
		binaryTree.postOrder();*//* // 2,5,4,3,1*/

      /*  System.out.println("删除节点");
        binaryTree.delNo(4);*/
        new GraphTree(root).setVisible(true);
        //前序遍历
        //前序遍历的次数 ：4
		System.out.println("前序遍历方式~~~");
        HeroTree resNode = binaryTree.preOrderSearch(5);
		if (resNode != null) {
			System.out.printf("找到了，信息为 no=%d name=%s", resNode.getNo(), resNode.getName());
		} else {
			System.out.printf("没有找到 no = %d 的英雄", 5);
		}

    }
}
//定义BinaryTree1 二叉树
class BinaryTree1{
    private HeroTree root;

    public void setRoot(HeroTree root) {
        this.root = root;
    }
    //前序遍历
    public void preOrder() {
        if (this.root!=null){
            this.root.preOrder();
        }else {
            System.out.println("二叉树为空");
        }
    }
    //中序遍历
    public void infixOrder() {
        if(this.root != null) {
            this.root.infixOrder();
        }else {
            System.out.println("二叉树为空，无法遍历");
        }
    }
    //后序遍历
    public void postOrder() {
        if(this.root != null) {
            this.root.postOrder();
        }else {
            System.out.println("二叉树为空，无法遍历");
        }
    }
    public void delNo(int no){
        if(this.root != null) {
            this.root.delNode(no);
        }else {
            System.out.println("二叉树为空，无法删除");
        }
    }
    public HeroTree preOrderSearch(int no){
        HeroTree heroTree = null;
        if (this.root!=null){
            heroTree=  this.root.preOrderSearch(no);
            System.out.println("jjjjjjjj:"+heroTree);
        }else {
            System.out.println("二叉树为空");
        }
        return heroTree;
    }
}



class HeroTree{
    private int no;
    private String name;
    private int layer;//
    private HeroTree left; //默认null
    private HeroTree right; //默认null
    public HeroTree(int no, String name) {
        this.no = no;
        this.name = name;
    }
    public int getNo() {
        return no;
    }
    public void setNo(int no) {
        this.no = no;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public HeroTree getLeft() {
        return left;
    }
    public void setLeft(HeroTree left) {
        this.left = left;
        left.setLayer(this.getLayer()+1);
    }
    public HeroTree getRight() {
        return right;
    }
    public void setRight(HeroTree right) {
        this.right = right;
        right.setLayer(this.getLayer()+1);
    }
    public int getLayer() {
        return layer;
    }
    public void setLayer(int layer) {
        this.layer = layer;
    }

    @Override
    public String toString() {
        return "HeroTree{" +
                "no=" + no +
                ", name='" + name + '\'' +
                ", layer=" + layer +
                '}';
    }


    //前序遍历
    public void preOrder(){
        System.out.println(this);

        if (this.left!=null){
            this.left.preOrder();
        }
        if (this.right!=null){
            this.right.preOrder();
        }
    }
    //int p=150;//步长
    int c=20; //半径
    int l=15;  //文字位置
    private final int R =40; //圆形的半径

    //前序遍历
    public void preOrder(Graphics g,int px,int py){
      //  System.out.println(this);
        //根据层数构建两个父子节点的距离
        int p=150/(this.layer+1);
        //构建圆形
        g.drawOval(px,py,R,R);
        //画字符串
        g.drawString(this.no +" "+this.name,l+px,l+py);
        if (this.left!=null){
            this.left.preOrder(g,px-p,py+p);
            //画线条
            g.drawLine(c+px,c+py, c+px-p,c+py+p);
        }
        if (this.right!=null){
            this.right.preOrder(g,px+p,py+p);
            //画线条
            g.drawLine(c+px,c+py, c+px+p,c+py+p);

        }
    }
    //中序遍历
    public void infixOrder(){
        if (this.left!=null){
            this.left.infixOrder();
        }
        System.out.println(this);
        if (this.right!=null){
            this.right.infixOrder();
        }
    }
    //后续遍历
    public void postOrder(){
        if (this.left!=null){
            this.left.postOrder();
        }
        if (this.right!=null){
            this.right.postOrder();
        }
        System.out.println(this);

    }
    //递归删除结点
    //1.如果删除的节点是叶子节点，则删除该节点
    //2.如果删除的节点是非叶子节点，则删除该子树
    public void delNode(int no) {
        //思路
		/*
		 * 	1. 因为我们的二叉树是单向的，所以我们是判断当前结点的子结点是否需要删除结点，而不能去判断当前这个结点是不是需要删除结点.
			2. 如果当前结点的左子结点不为空，并且左子结点 就是要删除结点，就将this.left = null; 并且就返回(结束递归删除)
			3. 如果当前结点的右子结点不为空，并且右子结点 就是要删除结点，就将this.right= null ;并且就返回(结束递归删除)
			4. 如果第2和第3步没有删除结点，那么我们就需要向左子树进行递归删除
			5.  如果第4步也没有删除结点，则应当向右子树进行递归删除.

		 */
        if(this.left!=null&&this.left.no==no){
            this.left=null;
            return;
        }
        if (this.right!=null&&this.right.no==no){
            this.right=null;
            return;
        }
        if (this.left!=null){
            this.left.delNode(no);
        }
        if (this.right!=null){
            this.right.delNode(no);
        }
    }
    //前序遍历查找
    /**
     *
     * @param no 查找no
     * @return 如果找到就返回该Node ,如果没有找到返回 null
     */
    public HeroTree preOrderSearch(int no) {
        System.out.println("进入前序遍历 : "+this);
        HeroTree heroTree = null;
        if (this.no==no){
            System.out.println("111111111111111: "+this.no);
            return this;
        }
        if (this.left!=null){
            heroTree=  this.left.preOrderSearch(no);
        }
        if (heroTree!=null){
            return heroTree;
        }
        if (this.right!=null){
            heroTree=this.right.preOrderSearch(no);
        }
        return heroTree;
    }

    //中序遍历查找
    public HeroTree infixOrderSearch(int no) {
        //判断当前结点的左子节点是否为空，如果不为空，则递归中序查找
        HeroTree heroTree = null;
        if (this.left!=null){
            heroTree=this.left.infixOrderSearch(no);
        }
        if (heroTree!=null){
            return heroTree;
        }
        System.out.println("进入中序遍历");
        //如果找到，则返回，如果没有找到，就和当前结点比较，如果是则返回当前结点
        if (this.no==no){
            return this;
        }
        //否则继续进行右递归的中序查找
        if (this.right!=null){
            heroTree=this.right.infixOrderSearch(no);
        }
        return heroTree;

    }
}